package com.offcn.dao.daoimpl;

import com.offcn.dao.BaseDao;
import com.offcn.dao.CourseUserDao;
import com.offcn.pojo.CourseUser;
import com.offcn.pojo.User;
import com.offcn.util.PageUtil;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class CourseUserDaoImpl extends BaseDao<CourseUser> implements CourseUserDao {

    /*获取分页查询人数*/
    @Override
    public int getCount(List<Integer> list) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("SELECT COUNT(*) FROM course_user ");
        if (list.size() != 0) {
            stringBuilder.append("WHERE uid IN (" + StringUtils.join(list, ",") + ")");
        }
        String sql = stringBuilder.toString();
        int number = getNumber(sql);
        return number;
    }

    @Override
    public List<CourseUser> queryAllByPage(PageUtil<CourseUser> courseUserPageUtil, List<Integer> list) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("SELECT * FROM course_user ");
        if (list.size() != 0) {
            stringBuilder.append("WHERE uid IN (" + StringUtils.join(list, ",") + ") ");
        }
        stringBuilder.append(" LIMIT ?,?");
        String sql = stringBuilder.toString();
        List<CourseUser> courseUserListList = getBeanList(sql, courseUserPageUtil.getStartIndex(), courseUserPageUtil.getPageSize());
        return courseUserListList;
    }

    //修改功能
    @Override
    public int updateCourse(CourseUser courseUser) {
        String sql = "UPDATE `course_user` SET `cid` = ? WHERE `id` = ? ";
        int update = update(sql, courseUser.getCid(), courseUser.getId());
        return update;
    }

    //批量删除功能
    @Override
    public int delAllCourse(String ids) {
        String sql = "DELETE FROM course_user WHERE id IN (" + ids + ")";
        int del = update(sql);
        return del;
    }
    //增加购买订单
    @Override
    public int insertCourseUser(Integer cid, Integer uid, String orderName) {
        String sql = "INSERT INTO course_user(cid,uid,orderNum) VALUE(?,?,?)";
        int update = update(sql, cid, uid, orderName);
        return update;
    }
}
